package controller.states;

import java.awt.Color;
import java.awt.Point;
import java.io.IOException;
import java.net.URISyntaxException;

import util.LevelUtils;

import model.GameModel;
import model.entities.IEntity;
import controller.BankType;
import controller.GameController;
import controller.IPlayerAction;
import controller.IState;
import controller.actions.BinAction;
import controller.actions.EntityAction;
import controller.actions.EntityClickedAction;

/**
 * This state is active when a player is in game
 * This is the root in game state
 *
 */
public class InGameState implements IState {

	private GameController controller;
	
	public InGameState(GameController controller) {
		this.controller = controller;
	}
	
	@Override
	public void handleAction(IPlayerAction playerAction) {}

	@Override
	public void handleEntityClickedAction(EntityClickedAction action) {
		GameModel model = controller.getModel();
		IEntity entity = model.getGameBoard().getEntityAtCell(action.getCell());
		
		if (entity == null)
			return;
		
		entity.handleClick(action.getButton());
		model.setDirty();
	}

	@Override
	public void handleEntityDraggedAction(EntityAction action) {
		Point cell = action.getCell();
		IEntity entity = controller.getModel().getGameBoard().getEntityAtCell(cell);
		if(entity == null)
			return;
//		if (!entity.isMovable()) // TODO add level builder case here
//			return;
		controller.getView().setCursorToIconAtCell(cell);
		controller.getView().setGridOn(true);
		controller.getView().laserAnimationOn(false);
		controller.getModel().getGameBoard().removeEntity(cell);
		controller.getModel().setDirty();
		controller.setState(new PlaceEntityState(controller, entity));
	}

	@Override
	public void handleEntityReleasedAction(EntityAction action) {}

	@Override
	public void handleBinClicked(BinAction action) {}

	@Override
	public void handleBinDraggedAction(BinAction action) {
		BankType type = action.getType();
		IEntity entity = null;
		switch (type) {
		case MIRROR :
			entity = controller.getModel().getBin().takeMirror(); break;
		case SOURCE : 
			if (action.getColor().equals(Color.RED)) {
				entity = controller.getModel().getBin().takeRedLaserSource();
			} 
			else if(action.getColor().equals(Color.GREEN)) {
				entity = controller.getModel().getBin().takeGreenLaserSource();
			}
			else if(action.getColor().equals(Color.BLUE)) {
				entity = controller.getModel().getBin().takeBlueLaserSource();
			}
			break;
			
		}

		if (entity == null)
			return;
		
		controller.getView().setCursorIcon(action.getIcon());	
		controller.getView().setGridOn(true);
		controller.getView().laserAnimationOn(false);
		controller.getModel().setDirty();
		controller.setState(new PlaceEntityState(controller, entity));
		
	}

	@Override
	public void handleLevelLoadAction(String levelName) {
		try {
			LevelUtils util =  new LevelUtils(); // TODO: LevelUtils should be static but doing this for applet patch up
			util.parseLevel(levelName, controller.getModel());
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		controller.getModel().setDirty();
	}

}
